package com.fr.report.write.config;

import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.data.core.DataUtils;
import com.fr.data.core.db.DBUtils;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import com.fr.data.core.db.dml.Delete;
import com.fr.data.core.db.dml.Insert;
import com.fr.data.core.db.dml.InsertOrUpdate;
import com.fr.data.core.db.dml.Select;
import com.fr.data.core.db.dml.Table;
import com.fr.data.core.db.dml.Update;
import com.fr.data.core.db.dml.Where;
import com.fr.report.script.Calculator;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class IntelliDMLConfig extends DMLConfig
{
  public void dmlSubmit(Connection paramConnection, Calculator paramCalculator, Object[] paramArrayOfObject, Map paramMap)
    throws SQLException
  {
    int i = getColumnConfigCount();
    if ((i == 0) || (this.table == null))
      return;
    Dialect localDialect = DialectFactory.generateDialect(paramConnection);
    Where localWhere = generateWhere(paramConnection, paramCalculator, paramArrayOfObject);
    if (Where.validate(localWhere))
    {
      if (doDelete(paramCalculator))
      {
        localObject1 = new Delete(this.table, localWhere, localDialect);
        ((Delete)localObject1).execute(paramConnection);
        return;
      }
      localObject1 = new Update(this.table, localWhere, localDialect);
      addColumn2InsertOrUpdate((InsertOrUpdate)localObject1, paramCalculator, paramArrayOfObject, paramConnection);
      if ((Update.validate((Update)localObject1)) && (((Update)localObject1).execute(paramConnection) > 0))
        return;
    }
    Object localObject1 = new Insert(this.table, localDialect);
    addColumn2InsertOrUpdate((InsertOrUpdate)localObject1, paramCalculator, paramArrayOfObject, paramConnection);
    ((Insert)localObject1).execute(paramConnection);
    if (paramMap != null)
    {
      String str = null;
      int j = 0;
      int k = getColumnConfigCount();
      while (j < k)
      {
        ColumnConfig localColumnConfig = getColumnConfig(j);
        if ((localColumnConfig.isKey()) && (((Insert)localObject1).isSelfincreaseColumn(localColumnConfig.getColumnName())))
        {
          str = localColumnConfig.getColumnName();
          break;
        }
        ++j;
      }
      if (str != null)
      {
        j = 0;
        k = 0;
        int l = ((Insert)localObject1).getColumnCount();
        while (k < l)
        {
          if (ComparatorUtils.equals(((Insert)localObject1).getColumnName(k), str))
          {
            j = ((Insert)localObject1).getColumnType(k);
            break;
          }
          ++k;
        }
        Object localObject2 = findRecordInsertedBySelfincreaseKey(str, j, localDialect, paramConnection);
        if (localObject2 != null)
          paramMap.putAll(findRecordByKeyAndValue(str, localObject2, paramConnection, localDialect));
      }
    }
  }

  private Object findRecordInsertedBySelfincreaseKey(String paramString, int paramInt, Dialect paramDialect, Connection paramConnection)
  {
    Object localObject = null;
    try
    {
      String str = paramDialect.getIdentitySelectString(paramDialect.table2SQL(this.table), paramString, paramInt);
      PreparedStatement localPreparedStatement = paramConnection.prepareStatement(str);
      ResultSet localResultSet = localPreparedStatement.executeQuery();
      localResultSet.next();
      localObject = localResultSet.getObject(1);
      localPreparedStatement.close();
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return localObject;
  }

  private Map findRecordByKeyAndValue(String paramString, Object paramObject, Connection paramConnection, Dialect paramDialect)
  {
    int i;
    HashMap localHashMap = new HashMap();
    String[] arrayOfString = DBUtils.checkInColumnNames(paramConnection, DataUtils.createSelectSQL(this.table, null, paramDialect), 0);
    if (arrayOfString.length == 0)
      return localHashMap;
    Where localWhere = new Where();
    localWhere.addColumn(paramString, paramObject);
    Select localSelect = new Select(this.table, localWhere, paramDialect);
    ResultSet localResultSet = null;
    PreparedStatement localPreparedStatement = null;
    try
    {
      localPreparedStatement = localSelect.createPreparedStatement(paramConnection);
      localResultSet = localPreparedStatement.executeQuery();
      if (localResultSet.next())
        for (i = 0; i < arrayOfString.length; ++i)
          localHashMap.put(arrayOfString[i], localResultSet.getObject(arrayOfString[i]));
    }
    catch (SQLException localSQLException)
    {
      FRContext.getLogger().log(Level.WARNING, localSQLException.getMessage(), localSQLException);
    }
    finally
    {
      DBUtils.closeResultSet(localResultSet);
      DBUtils.closeStatement(localPreparedStatement);
    }
    return localHashMap;
  }

  private boolean doDelete(Calculator paramCalculator)
  {
    for (int i = 0; i < getColumnConfigCount(); ++i)
    {
      ColumnConfig localColumnConfig = getColumnConfig(i);
      if (!(localColumnConfig.isKey()))
        break label36:
      if (localColumnConfig.isDeleted(paramCalculator))
        return true;
    }
    label36: return false;
  }

  private void addColumn2InsertOrUpdate(InsertOrUpdate paramInsertOrUpdate, Calculator paramCalculator, Object[] paramArrayOfObject, Connection paramConnection)
  {
    for (int i = 0; i < getColumnConfigCount(); ++i)
    {
      ColumnConfig localColumnConfig = getColumnConfig(i);
      if ((localColumnConfig.isKey()) && (paramInsertOrUpdate.isUpdate()))
        break label87:
      if ((localColumnConfig.isSkipUnmodified()) && (!(localColumnConfig.isModified(paramCalculator))))
        break label87:
      String str = localColumnConfig.getColumnName();
      int j = getColumnType(str, paramConnection);
      paramInsertOrUpdate.addColumn(str, j, paramArrayOfObject[i]);
    }
    label87:
  }

  public String toString()
  {
    return "update/insert into " + ((this.table == null) ? "null" : this.table.toString()) + " " + super.toString();
  }
}